package com.tiezhu.springdemo.controller;


import cn.hutool.core.util.RandomUtil;
import com.tiezhu.springdemo.Redis.MD5Util;
import com.tiezhu.springdemo.Redis.RedisConfig;
import com.tiezhu.springdemo.Redis.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;


@RestController
@RequestMapping("/Code")
public class CodeController {

    private static final String BASE_CHECK_CODES = "qwertyuiplkjhgfdsazxcvbnmQWERTYUPLKJHGFDSAZXCVBNM1234567890";


    @Autowired
    private RedisUtil redisUtil;

    @GetMapping("/getCheckCode")
    @ResponseBody
    private Map<String,Object> getCheckCode(){
        Map<String,Object> hashMap= new LinkedHashMap<>();
        String code = RandomUtil.randomString(BASE_CHECK_CODES,4);
        String key = MD5Util.MD5Encode(code+System.currentTimeMillis(), "utf-8");
        redisUtil.set(key,code,60);
        hashMap.put("status",1);
        hashMap.put("key",key);
        hashMap.put("code",code);
        return hashMap;
    }
}
